package com.hsogoo.ultraman.spring.refreshable;

import lombok.Data;
import lombok.Getter;
import lombok.ToString;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

/**
 * @Author huangsaigang
 * Created @2018/9/23.
 */
@Getter
@ToString
public class RefreshableConfig {
    private Field field;
    private Object bean;
    private String beanName;
    private String key;
    private String placeholder;
    private Class<?> targetType;

    public RefreshableConfig(String key, String placeholder, Object bean, String beanName, Field field){
        this.bean = bean;
        this.beanName = beanName;
        this.field = field;
        this.key = key;
        this.placeholder = placeholder;
        this.targetType = field.getType();
    }

    public void refresh(Object newVal) throws IllegalAccessException {
        boolean accessible = field.isAccessible();
        field.setAccessible(true);
        field.set(bean, newVal);
        field.setAccessible(accessible);
    }
}
